Ext.Loader.setConfig({enabled: true});
Ext.Loader.setPath({'Ext.ux' : 'extjs4/ux'});
Ext.define('TS.view.rp.RpARecBillSettleSelector',{
	extend:'Ext.window.Window',
	alias:'widget.arecbillselector',
	title:'应收单选择',
	width:900,
	height:500,
	border: false,
	bodyPadding: 0,
	modal:true,
    defaults: {
        anchor: '100%',
        margins: '0 0 0 0'
    },
	mixins: {
        observable: 'Ext.util.Observable'
    },
    constructor: function (config) {
    	this.callParent();
    	this.initConfig(config);
        this.addEvents(
        {'selected':true}
        );
    },
    config: {
    	custId:'',//客户Id
    	custName:'xxx'//客户名称
    },
	initComponent:function() {
//	    var cellEditing = Ext.create('Ext.grid.plugin.CellEditing', {
//	        clicksToEdit: 1
////	        listeners:{
////	            'beforeedit': function(editor,obj,opt) {
////	            	if(obj.column && obj.column.dataIndex == "currentCheckAmount") { 
////	                    return false; 
////	                }
////	            } 
////	        }
//	    });
	    
		Ext.apply(this,{
			items: [
			        {
			        	xtype:'panel',
			        	height:50,
			        	html : '<h2>[' + this.custName + ']的应收单</h2>',
			        	bodyStyle:{
			        		color:'blue'
			        	}
			        },
			        {
			    	 	xtype:'grid',
						header:false,
						border:false,
						margins: '0 0 0 0',
						selType:'checkboxmodel',//设定选择模式
						selModel:{selType:'checkboxmodel',checkOnly:true},
						multiSelect:true,//运行多选
					    features: [{
					        ftype: 'summary'
					    }],
//					    plugins: [
//					        cellEditing
//					    ],
						columns:[
						    {header:'单据类型',dataIndex:'arpBillName',summaryType:'count',summaryRenderer: function(value, summaryData, dataIndex) {return '<b>合计:</b>'; }},
						    {header:'单据编号',dataIndex:'arpBillCode'},
						    {header:'单据日期',dataIndex:'billDate',renderer:Ext.util.Format.dateRenderer('Y-m-d')},
						    {header:'应收金额',dataIndex:'amount',align:'right',summaryType:'sum',summaryRenderer:function(value,summaryData,dataIndex){return '<b>' + Ext.util.Format.number(value,'0,000.00') + '</b>';}},
						    {header:'已结算金额',dataIndex:'checkAmount',align:'right',summaryType:'sum',summaryRenderer:function(value,summaryData,dataIndex){return '<b>' + Ext.util.Format.number(value,'0,000.00') + '</b>';}},
						    {header:'余额',dataIndex:'unCheckAmount',align:'right',summaryType:'sum',summaryRenderer:function(value,summaryData,dataIndex){return '<b>' + Ext.util.Format.number(value,'0,000.00') + '</b>';}},
						    {
						    	header:'本次结算金额',
						    	align:'right',
						    	dataIndex:'currentCheckAmount',
						    	summaryType:'sum',
						    	summaryRenderer:function(value,summaryData,dataIndex){return '<b>' + Ext.util.Format.number(value,'0,000.00') + '</b>';},
						    	renderer : Ext.util.Format.numberRenderer('0.00')
//				                editor: {
//				                    xtype: 'numberfield',
//				                    allowBlank: false,
//	                	        	fieldStyle:'text-align:right',
//  	                	        	allowDecimals: true
//				                }
						    },
						    {header:'单据备注',dataIndex:'remark',width:120}
						],
						listeners:{
							'select':function(selModel,record,index) {
								record.set('currentCheckAmount',record.get('unCheckAmount'));
								selModel.getStore().commitChanges();
								this.getView().refresh();
							},
							'deselect':function(selModel,record,index) {
								record.set('currentCheckAmount',0);
								selModel.getStore().commitChanges();
								this.getView().refresh();
							}
//							'beforeselect':function(selModel, record, index, eOpts ) {
//								var position = selModel.getCurrentPosition();
//								if(position.column.dataIndex=='currentCheckAmount') {
//									 return false;
//								}
//							}
						}
			     }
			],
			buttons:[
			      {
			    	  xtype:'button',iconCls:'button-ok',action:'ok',text:'确定',
			    	  handler:function(button) {
			    		  var window = button.up('window');
			    		  var grid = window.down('grid');
			    		  var records = grid.getSelectionModel().getSelection();
			    		  if(records.length == 0) {
			    			  Ext.Msg.show({
			    				  title:'ThinkGsp',
			    				  msg:'您没有选择任何应收单!',
			    				  buttons:Ext.Msg.OK,
			    				  icon:Ext.Msg.WARNING
			    			  });
			    			  return;
			    		  }
			    		  window.fireEvent('selected',records);
			    		  window.close();
			    	  }
			      },
			      {xtype:'button',iconCls: 'button-close',text:'取消',handler:function(){this.up('window').close();}}
			]
		});
		this.callParent(arguments);
	},
	listeners: {
		show:{
			fn:function() {
				if(this.custName) {
					this.down('panel').update('<h2>[' + this.custName + ']的应收单</h2>');
					//this.down('panel').set('heml','<h2>[' + this.custId + '|' + this.custName + ']的应收单</h2>');
				}
				var gridStore = Ext.create('Ext.data.Store',{
					model:'TS.model.rp.ArpBillModel',
					proxy:{
						url:'rp/recbill/getUnSettleARecBill.do',
						type:'ajax',
						reader: {
							type:'json',
							root:''
						}
					},
					autoLoad:false
				});
				gridStore.load({
					params:{custId:this.custId}
				});
				var grid = this.down('grid');
				grid.reconfigure(gridStore);
			}
		}
	}
});